package org.jeecg.modules.bl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.bl.entity.BlDiscounts;
import org.jeecg.modules.bl.entity.BlSubDiscounts;
import org.jeecg.modules.bl.mapper.BlDiscountsMapper;
import org.jeecg.modules.bl.mapper.BlSubDiscountsMapper;
import org.jeecg.modules.bl.service.IBlDiscountsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 水费优惠方案
 * @Author: jeecg-boot
 * @Date:   2022-02-13
 * @Version: V1.0
 */
@Service
public class BlDiscountsServiceImpl extends ServiceImpl<BlDiscountsMapper, BlDiscounts> implements IBlDiscountsService {

	@Resource
	private BlDiscountsMapper blDiscountsMapper;
	@Resource
	private BlSubDiscountsMapper blSubDiscountsMapper;

	@Override
	@Transactional
	public void saveMain(BlDiscounts blDiscounts, List<BlSubDiscounts> blSubDiscountsList) {
		blDiscountsMapper.insert(blDiscounts);
		if(blSubDiscountsList!=null && blSubDiscountsList.size()>0) {
			for(BlSubDiscounts entity:blSubDiscountsList) {
				//外键设置
				entity.setDiscountId(blDiscounts.getId());
				blSubDiscountsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(BlDiscounts blDiscounts,List<BlSubDiscounts> blSubDiscountsList) {
		blDiscountsMapper.updateById(blDiscounts);

		//1.先删除子表数据
		blSubDiscountsMapper.deleteByMainId(blDiscounts.getId());

		//2.子表数据重新插入
		if(blSubDiscountsList!=null && blSubDiscountsList.size()>0) {
			for(BlSubDiscounts entity:blSubDiscountsList) {
				//外键设置
				entity.setDiscountId(blDiscounts.getId());
				blSubDiscountsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		blSubDiscountsMapper.deleteByMainId(id);
		blDiscountsMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			blSubDiscountsMapper.deleteByMainId(id.toString());
			blDiscountsMapper.deleteById(id);
		}
	}

	@Override
	public List<BlSubDiscounts> queryDiscountItemByCusId(String cusId) {
		return blDiscountsMapper.queryDiscountItemByCusId(cusId);
	}

}
